<?php
namespace api\admin\controller;

use cmf\controller\RestBaseController;
// use think\Validate;
use api\admin\model\ToiletModel;
// use think\Db;

header("Access-Control-Allow-Origin: *"); // Support CORS
header('Access-Control-Allow-Headers:x-requested-with,content-type,Device-Type,User-Token');
header('Access-Control-Allow-Methods:GET, POST, PATCH, PUT, DELETE');
header('Access-Control-Allow-Headers:Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-Requested-With');

class AgentController extends RestBaseController
{

    protected $beforeActionList = [
        'getAuthType',
        // 'toiletcheck' => ['only' => 'addtoilet'],
    ];
    /**
     * [getAuthType 权限判断]
     * @Author   HUI
     * @DateTime 2019-01-07
     * @version  [version]
     * @param    string     $value [description]
     * @return   [type]            [description]
     */
    public function getAuthType($value = '')
    {
        //判断是否登陆
        $this->getUserType();
        //判断是否为商家
        if ($this->getUserType() !== 2) {
            $this->error('暂无权限.....');
        }else{
            return $this->getUserParentStatus();
        }
    }

    /**
     * [getToiletlist 获取代理商公厕列表]
     * @Author   HUI
     * @DateTime 2019-01-21
     * @version  [version]
     * @param    string     $value [description]
     * @return   [type]            [description]
     */
    public function getToiletlist($value='')
    {
        $param = $this->request->param();
        $Toilet = new ToiletModel();
        $where = ['u.parent_id' => $this->getUserId(), 'a.status' => '1'];
        if ($this->request->isPost() && !empty($param['keywords'])) {
            $where['a.title||o.user_info_name'] = ['like', "%{$param['keywords']}%"];
        }
        $result = $Toilet
        ->alias('a')
        ->field('a.id,a.title,a.position,a.man_num,a.woman_num,a.flow_num,a.sensor_num,a.mac,a.start_time,a.end_time,a.create_time,o.*')
        ->join('user u','u.id = a.user_id')
        ->join('user_other_info o','o.user_id = a.user_id')
        ->where($where)
        ->paginate(10); 
        //查询公厕状态
        foreach ($result as $key => $value) {
            $value['cloud_status'] = $Toilet->getCloudStatus($value['mac']);
            $result[$key] = $value;
        }
        return $this->success('获取成功', ['details' => $result
            , 'device' => $this->getDeviceCountNum(), 'show_code' => $this->getUserCode(),
        ]);
    }


}